package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import server.BkCAccITF;
import server.PersonInterface;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;



import client.Configuration;

public class BkUserLoginGUI extends JFrame {

	private JPanel contentPane;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	Configuration config=new Configuration();
	String rmiAddress=config.getRmiAddress();
	BkCAccITF CAitf;
    Person ps = new Person();
	int pID = ps.getPID();
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BkUserLoginGUI frame = new BkUserLoginGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BkUserLoginGUI() {
		setTitle("\u7528\u6237\u767B\u5F55");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(93, 111, 170, 36);
		contentPane.add(passwordField);
		
		
		JLabel label_1 = new JLabel("请输入您的登录密码：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_1.setBounds(31, 33, 221, 27);
		contentPane.add(label_1);
		
		JButton button = new JButton("\u6CA1\u6709\u8D26\u6237\uFF1F\u6CE8\u518C");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BkUserRegisterGUI frame = new BkUserRegisterGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				BkUserLoginGUI.this.dispose();
			}
		});
		button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button.setBounds(286, 198, 126, 27);
		contentPane.add(button);
		
		JButton btnNewButton = new JButton("\u767B\u5F55");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {	
				try {
					CAitf = (BkCAccITF) Naming.lookup(rmiAddress+"BankCurrentAccount");
					String skey = passwordField.getText().toString();
					
					if(skey.equals(""))
						JOptionPane.showMessageDialog(null, "输入不能为空。");
					else{
						int pkey = Integer.parseInt(skey);
						System.out.print(pkey+"");
						if(!CAitf.checkKey(pID, pkey))
							JOptionPane.showMessageDialog(null, "用户名或密码错误！");
						else{
							BankAccountGUI frame = new BankAccountGUI();
							frame.setLocationRelativeTo(null);
							frame.setVisible(true);
							BkUserLoginGUI.this.dispose();
						}
					}
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				}
		});
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		btnNewButton.setBounds(284, 105, 74, 45);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u8FD4\u56DE");
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				HomePageGUI frame = new HomePageGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		btnNewButton_1.setBounds(341, 10, 83, 22);
		contentPane.add(btnNewButton_1);
	}
}
